home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 842 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  71 lines

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: To malloc (new) or not to malloc?  When is the question.
  5. Date: 09 Jan 1996 16:18:57 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Jan9091857@qcd.lanl.gov>
  8. References: <4ctvk3$ort@maverick.tad.eds.com>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: fignet05.darrins@eds.com's message of 9 Jan 1996 14:54:59 GMT
  13.  
  14. --text follows this line--
  15. In article <4ctvk3$ort@maverick.tad.eds.com> fignet05.darrins@eds.com
  16. (Darrin Smith) writes: 
  17. <snip>
  18.    Why is it that you can do something like the following:
  19.  
  20.        char *x;
  21.  
  22.        x="Some really long string with no particular meaning";
  23.  
  24.    and have no problems, but can't (safely) do something like this:
  25.  
  26. <snip>
  27.  
  28.        st1 *sptr;    //or struct st1 *sptr in C instead of C++
  29.  
  30.        sptr=fread(....);
  31.  
  32. <snip>
  33.  
  34.    What is going on here?  Why isn't memory set aside for st1 *sptr just as 
  35.    it is for char *x?  Before I did the new (or malloc) it seemed as though 
  36.    my program was getting written over!
  37.  
  38. Neither is setting aside memory (or rather, to point out an erroneous
  39. wording on your part, both are setting aside memory for the pointer:
  40. but the pointer is not pointing anywhere when it is declared.
  41.  
  42. "Some ... meaning" is a string literal: and string literals in almost
  43. all uses are arrays of characters. `Array' always has memory allocated
  44. to it: the characters comprising a string literal sit in statically
  45. allocated unmodifiable memory. 
  46.  
  47. x = "Some ... meaning" sets the pointer x to be the address of the
  48. first character of this array.
  49.  
  50. Similarly if `fread(....)' did return a valid pointer,
  51. sptr=fread(....) would have been valid. Of course the four dots inside
  52. the parentheses after fread make absolutely no sense, and it does not
  53. return a pointer either.
  54.  
  55. (BEFORE asking questions, read the FAQ available at rtfm.mit.edu. If
  56. you need to as a question, you should provide a small, complete
  57. compilable program with a description of what you expected and what
  58. happened. sptr=fread(....) is just not acceptable.)
  59.  
  60.    Why?
  61.  
  62. Cheers
  63. Tanmoy
  64. --
  65. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  66. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  67. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  68. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  69. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  70. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  71.